﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Models
{
    public class OrderViewModel
    {
        IRestaurantService _restaurantService;
        IFoodService _foodService;
        IDinnerTableService _dinnerTableService;
        IShoppingCartService _shoppingCartService;
        IUserService _userService;
        IOrderFoodService _orderFoodService;
        public OrderViewModel()
        {
            _userService = new UserService();
            _restaurantService = new RestaurantService();
            _foodService = new FoodService();
            _dinnerTableService = new DinnerTableService();
            _shoppingCartService = new ShoppingCartService();
            _orderFoodService = new OrderFoodService();
        }
        public int Id { get; set; }

        [Display(Name = "订单号")]
        public string OrderNo { get; set; }

        [Display(Name = "订单时间")]
        public System.DateTime OrderTime { get; set; }

        /// <summary>
        /// 0未支付，1已支付，2已确认，3正在操作，4已上菜，5已完成，6已取消
        /// </summary>
        [Display(Name = "订单状态")]
        public short Status { get; set; }
        private List<SelectListItem> statusItems;
        public List<SelectListItem> StatusItems
        {
            get
            {
                if (statusItems == null)
                {
                    List<StatusModel> statusModels = new List<StatusModel>();
                    statusModels.Add(new StatusModel { Code = 0, Name = "未支付" });
                    statusModels.Add(new StatusModel { Code = 1, Name = "已支付" });
                    statusModels.Add(new StatusModel { Code = 2, Name = "已确认" });
                    statusModels.Add(new StatusModel { Code = 3, Name = "操作中" });
                    statusModels.Add(new StatusModel { Code = 4, Name = "已上菜" });
                    statusModels.Add(new StatusModel { Code = 5, Name = "已完成" });
                    statusModels.Add(new StatusModel { Code = 6, Name = "已取消" });

                    statusItems = new List<SelectListItem>();
                    foreach (var st in statusModels)
                    {
                        statusItems.Add(new SelectListItem
                        {
                            Text = st.Name,
                            Value = st.Code + "",
                            Selected = st.Code == this.Status

                        });
                    }
                }

                return statusItems;
            }
        }

        [Required]
        [DataType(DataType.Time)]
        [Display(Name = "用餐时间")]
        public System.DateTime MealTime { get; set; }

        /// <summary>
        /// 1 支付宝 2 微信
        /// </summary>
        [Required]
        [Display(Name = "支付方式")]
        public short PayType { get; set; }

        public string PayTypeString
        {
            get
            {
                if (this.PayType == 0)
                    return "支付宝";
                else if (this.PayType == 1)
                    return "微信";
                else
                    return "柜台";
            }
        }

        [Required]
        [Display(Name = "订餐人")]
        [StringLength(10, MinimumLength = 2, ErrorMessage = "{2}到{1}个字符")]
        public string BuyerName { get; set; }

        [Required]
        [Display(Name = "联系电话")]
        [StringLength(20, MinimumLength = 8, ErrorMessage = "{2}到{1}个字符")]
        public string BuyerPhone { get; set; }

        [Display(Name = "备注")]
        [StringLength(20, MinimumLength = 0, ErrorMessage = "{2}到{1}个字符")]
        public string Remark { get; set; }


        public int User_Id { get; set; }
        public int Restaurant_Id { get; set; }
        [Display(Name ="餐桌")]
        public int DinnerTable_Id { get; set; }

        private User user;
        public User User
        {
            get
            {
                if (user == null)
                    user = _userService.Find(User_Id);
                return user;
            }
        }

        private Restaurant restaurent;
        public Restaurant Restaurent
        {
            get
            {
                if (restaurent == null)
                    restaurent = _restaurantService.Find(this.Restaurant_Id);
                return restaurent;
            }
        }

        private DinnerTableViewModel dinnerTableViewModel;
        public DinnerTableViewModel DinnerTableViewModel
        {
            get
            {
                if (dinnerTableViewModel == null)
                {
                    var dt = _dinnerTableService.Find(this.DinnerTable_Id);
                    dinnerTableViewModel = DinnerTableViewModel.FromDinnerTable(dt);
                }

                return dinnerTableViewModel;
            }
        }
        //[Required]
        [Display(Name = "包房费用")]
        public decimal RoomPrice { get; set; }

        //[Required]
        [Display(Name = "餐位费")]
        public decimal MealFee { get; set; }

        [Display(Name = "总价")]
        public decimal TotalPrice { get; set; }

        private ShoppingCartsViewModel scsvm;
        public ShoppingCartsViewModel ShoppingCartsViewModel
        {
            get
            {
                if (scsvm == null)
                {
                    var carts = _shoppingCartService.FindList(50, this.User_Id, 0, 0, 0);

                    scsvm = new ShoppingCartsViewModel();

                    foreach (var cart in carts)
                    {
                        scsvm.ShoppingCarts.Add(ShoppingCartViewModel.FromShoppingCart(cart));
                    }
                }

                return scsvm;
            }
        }


        private List<OrderFood> foods;
        public List<OrderFood> Foods
        {
            get
            {
                if (foods == null)
                {
                    var ofrs = _orderFoodService.FindList(100, this.Id, 0);
                    foods = new List<OrderFood>();
                    foreach(var of in ofrs)
                    {
                        foods.Add(OrderFood.FromOrderFoodRel(of));
                    }

                    //foods = new List<Food>();
                    //var carts = _shoppingCartService.FindList(50, this.User_Id, 0, 0, 0);

                    //ShoppingCartsViewModel scsvm = new ShoppingCartsViewModel();

                    //foreach (var cart in carts)
                    //{
                    //    scsvm.ShoppingCarts.Add(ShoppingCartViewModel.FromShoppingCart(cart));
                    //}

                    //scsvm.ShoppingCarts.ForEach(sc=>foods.Add(sc.Food));

                }
                return foods;
            }
        }

        private Order order;
        public Order Order
        {
            get
            {
                if (order == null)
                {
                    Random r = new Random();
                    int n = r.Next(100000, 999999);
                    order = new Order()
                    {
                        Id = this.Id,
                        OrderNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + n,
                        OrderTime = DateTime.Now,
                        TotalPrice = this.TotalPrice,
                        Status = this.Status,
                        MealTime = this.MealTime,
                        RoomPrice = this.RoomPrice,
                        MealFee = this.MealFee,
                        PayType = this.PayType,
                        BuyerName = this.BuyerName,
                        BuyerPhone = this.BuyerPhone,
                        Remark = this.Remark,
                        User_Id = this.User_Id,
                        Restaurant_Id = this.Restaurant_Id,
                        DinnerTable_Id = this.DinnerTable_Id,
                    };
                }

                return order;
            }
        }

        public static OrderViewModel FromOrder (Order order)
        {
            return new OrderViewModel()
            {
                Id = order.Id,
                OrderNo = order.OrderNo,
                OrderTime = order.OrderTime,
                TotalPrice = order.TotalPrice,
                Status = order.Status,
                MealTime = order.MealTime,
                RoomPrice = order.RoomPrice,
                MealFee = order.MealFee,
                PayType = order.PayType,
                BuyerName = order.BuyerName,
                BuyerPhone = order.BuyerPhone,
                Remark = order.Remark,
                User_Id = order.User_Id,
                Restaurant_Id = order.Restaurant_Id,
                DinnerTable_Id = order.DinnerTable_Id,
            };
        }
    }

    internal class StatusModel
    {
        public string Name { get; set; }
        public int Code { get; set; }
    }
}